package com.example.myapplication.ui.register;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.databinding.RegisterFragmentBinding;
import com.example.myapplication.ui.login.MainActivity;
import com.example.myapplication.utils.MySqliteHelper;


/**
 *
 */
public class RegisterFragment extends Fragment {

    private RegisterViewModel mViewModel;
    private RegisterFragmentBinding binding;
    private String TAG="RegisterFragment 测试";
    public static RegisterFragment newInstance() {
        return new RegisterFragment();
    }

    /**
     * 初始化 注册数据
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {


        mViewModel = new ViewModelProvider(this).get(RegisterViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.register_fragment, container, false);
        binding.setData(mViewModel);
        binding.register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, mViewModel.getName().getValue());

                if(TextUtils.isEmpty(mViewModel.getName().getValue())){
                    Toast.makeText(getActivity(), "账户名为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!mViewModel.getPassword().getValue().equals(mViewModel.getTwo_password().getValue())){
                    Toast.makeText(getActivity(), "两次输入密码不一样", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 开始 注册用户
                MySqliteHelper mySqliteHelper=new MySqliteHelper(getActivity());
                SQLiteDatabase db =mySqliteHelper.getReadableDatabase();
                ContentValues cValue = new ContentValues();
                //添加用户名
                Log.d(TAG, mViewModel.getName().getValue());
                cValue.put("name",mViewModel.getName().getValue());
                //添加密码
                cValue.put("password",mViewModel.getPassword().getValue());
                Log.d(TAG, mViewModel.getPassword().getValue());

                cValue.put("age",18);
                cValue.put("information","");
                //调用insert()方法插入数据
                db.insert("user",null,cValue);
                Toast.makeText(getActivity(), "注册成功", Toast.LENGTH_SHORT).show();
                db.close();

                Intent intent = new Intent(getContext(),MainActivity.class);
                startActivity(intent);
            }
        });

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}